<?php

namespace app\wechat\controller\customer;

use app\admin\model\Authorizer;
use app\admin\model\BeanstalkTask;
use app\admin\model\CustomerMessage;
use app\wechat\controller\Base;
use app\wechat\controller\Openid;
use app\wechat\controller\Platform;
use Pheanstalk\Pheanstalk;
use think\Request;

// 客服消息相关
class Cusmsg extends Base
{
    protected $platform;
    public $openid_redis;

    public function __construct(Request $request = null)
    {
        parent::__construct($request);
        $this->platform = new Platform();
        $this->openid_redis = new Openid();
    }

    // 发送图文客服消息任务
    public function send_schedule(Request $request)
    {
        // 获取参数
        $id = $request->param('id');
        $send_type = $request->param('send_type');
        $send_date = $request->param('send_date');

        // 判断发送类型
        if ($send_type == 'send_now') {
            $this->async_send($id); // 直接发送
        } else if ($send_type == 'send_delay') {
            if ($send_date != '') { // 定时发送
                $this->async_send($id, $send_date);
            } else {
                $this->error('error');
            }
        } else {
            $this->error('error');
        }
    }

    // 异步发送文本客服消息
    public function async_send($msgid, $send_date = 0)
    {
        // 连接beanstalk
        $pheanstalk = new Pheanstalk('127.0.0.1');

        // 设置定时
        if ($send_date) {
            $delay = $send_date - time();
        } else {
            $delay = 0;
        }

        // 组合消息数据
        $msg = CustomerMessage::get($msgid);
        $type = $msg->type;
        $item = array(
            'title' => $msg->title,
            'image' => $msg->image,
            'url' => $msg->url,
            'content' => $msg->content,
        );

        $gh_list = Authorizer::whereIn('ghid', $msg->gh_list)->select();
        $job_list = array();

        // 循环公众号列表
        foreach ($gh_list as $gh) {
            $appid = $gh->appid;

            // 拼接数据
            $data = array(
                'appid' => $appid,
                'item' => $item,
                'type' => $type,
                'refresh_token' => $gh->refresh_token,
            );

            // 合并上次发送成功的openid
            $this->openid_redis->merge_openid($appid);

            // 设置发送任务
            $job = $pheanstalk->useTube('customer')->put(json_encode($data), 2, $delay, 600);

            // 保存job id
            array_push($job_list, $job);
        }

        // 将任务存入数据库
        $this->save_task($msgid, $job_list, $send_date);

        $this->success('ok');
    }

    // 存储任务
    public function save_task($msgid, $job_list, $send_date)
    {
        $task = new BeanstalkTask();
        $task->msg_id = $msgid;
        $job_str = '';
        foreach ($job_list as $job) {
            $job_str .= $job . ',';
        }
        $task->job_list = rtrim($job_str, ',');
        $task->delay = $send_date;
        $task->createtime = date('Y-m-d H:i:s', time());
        $task->save();
    }
}
